/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [ExaminationUtil.java]
 * Creation Date        [12-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_04_other.examApp.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 考试工具类
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class ExaminationUtil {

	/**
	 * 读取文件
	 * 
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static List<String> readFile(String fileName) throws IOException {
		List<String> infos = new ArrayList<String>();
		FileInputStream fis = null;
		BufferedReader br = null;

		try {
			fis = new FileInputStream(fileName);
			br = new BufferedReader(new InputStreamReader(fis));
			while (true) {
				String temp = br.readLine();
				if (temp == null) {
					break;
				} else {
					infos.add(temp);
				}
			}
		} catch (Exception e) {
			System.err.println("找不到配置文件");
			e.printStackTrace();
		} finally {
			if (br != null) {
				br.close();
			}
		}
		return infos;
	}

	/**
	 * 写出文件
	 * 
	 * @param infos
	 * @param fileName
	 * @throws IOException
	 */
	public static void writeFile(List<String> infos, String fileName)
			throws IOException {
		File file = new File(fileName);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream fos = null;
		PrintStream ps = null;
		try {
			if (infos != null) {
				fos = new FileOutputStream(file, true);
				ps = new PrintStream(fos);
				for (String info : infos) {
					ps.println(info);
				}
			}
		} catch (Exception e) {
			System.out.println("写出文件失败, 请重试");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				ps.close();
			}
		}
	}

}
